﻿namespace Rzrlf.SP.Innovator.Bdc.PartBdcModel
{
    public class PartEntityService
    {
        #region BDC Methods...

        public static PartEntity ReadItem(string Id)
        {
            foreach (PartEntity entity in GetAllEntities())
            {
                if (entity.id.Equals(Id, System.StringComparison.OrdinalIgnoreCase))
                {
                    return entity;
                }
            }

            return null;
        }

        public static System.Collections.Generic.IEnumerable<PartEntity> ReadList()
        {
            return GetAllEntities().ToArray();
        }

        public static System.Collections.Generic.List<PartEntity> GetAllEntities()
        {
            return ParseQueryResults(Rzrlf.SP.Innovator.Bdc.Utils.IomUtil.GetItems("Part"));
        }

        #endregion

        #region Helper Methods...

        private static System.Collections.Generic.List<PartEntity> ParseQueryResults(Aras.IOM.Item result)
        {
            System.Collections.Generic.List<PartEntity> entities = new System.Collections.Generic.List<PartEntity>();

            for (int i = 0; i < result.getItemCount(); i++)
            {
                Aras.IOM.Item item = result.getItemByIndex(i);

                PartEntity entity = new PartEntity();
                entity.classification = item.getProperty("classification", string.Empty);
                entity.config_id = item.getPropertyAttribute("config_id", "keyed_name", string.Empty);
                entity.cost = item.getProperty("cost", string.Empty) == string.Empty ? null : (decimal?)decimal.Parse(item.getProperty("cost"));
                entity.cost_basis = item.getProperty("cost_basis", string.Empty);
                entity.created_by_id = item.getPropertyAttribute("created_by_id", "keyed_name", string.Empty);
                entity.created_on = item.getProperty("created_on", string.Empty) == string.Empty ? null : (System.DateTime?)System.DateTime.Parse(item.getProperty("created_on"));
                entity.css = item.getProperty("css", string.Empty);
                entity.current_state = item.getProperty("current_state", string.Empty);
                entity.description = item.getProperty("description", string.Empty);
                entity.effective_date = item.getProperty("effective_date", string.Empty) == string.Empty ? null : (System.DateTime?)System.DateTime.Parse(item.getProperty("effective_date"));
                entity.generation = item.getProperty("generation", string.Empty) == string.Empty ? null : (int?)int.Parse(item.getProperty("generation"));
                entity.id = item.getAttribute("id", string.Empty);
                entity.is_current = item.getProperty("is_current", "0") == "1" ? true : false;
                entity.is_released = item.getProperty("is_released", "0") == "1" ? true : false;
                entity.item_number = item.getProperty("item_number", string.Empty);
                entity.item_type = item.getAttribute("type", string.Empty);
                entity.keyed_name = item.getProperty("keyed_name", string.Empty);
                entity.locked_by_id = item.getPropertyAttribute("locked_by_id", "keyed_name", string.Empty);
                entity.major_rev = item.getProperty("major_rev", string.Empty);
                entity.make_buy = item.getProperty("make_buy", string.Empty);
                entity.managed_by_id = item.getPropertyAttribute("managed_by_id", "keyed_name", string.Empty);
                entity.markup = item.getProperty("markup", string.Empty);
                entity.minor_rev = item.getProperty("minor_rev", string.Empty);
                entity.modified_by_id = item.getPropertyAttribute("modified_by_id", "keyed_name", string.Empty);
                entity.modified_on = item.getProperty("modified_on", string.Empty) == string.Empty ? null : (System.DateTime?)System.DateTime.Parse(item.getProperty("modified_on"));
                entity.mtbf = item.getProperty("mtbf", string.Empty) == string.Empty ? null : (int?)int.Parse(item.getProperty("mtbf"));
                entity.name = item.getProperty("name", string.Empty);
                entity.new_version = item.getProperty("new_version", "0") == "1" ? true : false;
                entity.not_lockable = item.getProperty("not_lockable", "0") == "1" ? true : false;
                entity.owned_by_id = item.getPropertyAttribute("owned_by_id", "keyed_name", string.Empty);
                entity.part_number = item.getProperty("part_number", string.Empty);
                entity.permission_id = item.getProperty("permission_id", string.Empty);
                entity.release_date = item.getProperty("release_date", string.Empty) == string.Empty ? null : (System.DateTime?)System.DateTime.Parse(item.getProperty("release_date"));
                entity.release_status = item.getProperty("release_status", string.Empty);
                entity.revision = item.getProperty("major_rev", string.Empty);
                entity.state = item.getProperty("state", string.Empty);
                entity.superseded_date = item.getProperty("superseded_date", string.Empty) == string.Empty ? null : (System.DateTime?)System.DateTime.Parse(item.getProperty("superseded_date"));
                entity.unit = item.getProperty("unit", string.Empty);
                entity.url_3d = item.getProperty("url_3d", string.Empty);
                entity.url_thumbnail = item.getProperty("url_thumbnail", string.Empty);
                entity.url_workspace = item.getProperty("url_workspace", string.Empty);
                entity.weight = item.getProperty("weight", string.Empty) == string.Empty ? null : (decimal?)decimal.Parse(item.getProperty("weight"));
                entity.weight_basis = item.getProperty("weight_basis", string.Empty);
                entity.work_stream = item.getProperty("work_stream", string.Empty);
                
                entities.Add(entity);
            }

            return entities;
        }

        #endregion
    }
}
